fn infix_to_rpn(infix: &str) -> String {
    let mut output_queue: Vec<char> = Vec::new();
    let mut operator_stack: Vec<char> = Vec::new();

    let precedence = |operator: char| match operator {
        '+' | '-' => 1,
        '*' | '/' => 2,
        _ => 0,
    };

    for token in infix.chars() {
        match token {
            '0'..='9' => output_queue.push(token),
            '+' | '-' | '*' | '/' => {
                while !operator_stack.is_empty()
                    && operator_stack.last().map(|&op| precedence(op) >= precedence(token))
                    == Some(true)
                    && *operator_stack.last().unwrap() != '('
                {
                    output_queue.push(operator_stack.pop().unwrap());
                }
                operator_stack.push(token);
            }
            '(' => operator_stack.push(token),
            ')' => {
                while operator_stack.last() != Some(&'(') {
                    if let Some(operator) = operator_stack.pop() {
                        output_queue.push(operator);
                    } else {
                        panic!("Mismatched parentheses");
                    }
                }
                operator_stack.pop(); // Pop the '('
            }
            _ => panic!("Invalid token: [{}]", token),
        }
    }

    while let Some(operator) = operator_stack.pop() {
        if operator == '(' {
            panic!("Mismatched parentheses");
        }
        output_queue.push(operator);
    }

    output_queue.iter().collect()
}

fn evaluate_rpn(rpn: &str) -> i32 {
    let mut stack: Vec<i32> = Vec::new();

    for token in rpn.split_whitespace() {
        match token {
            "+" => {
                if let (Some(b), Some(a)) = (stack.pop(), stack.pop()) {
                    stack.push(a + b);
                } else {
                    panic!("Invalid expression");
                }
            }
            "-" => {
                if let (Some(b), Some(a)) = (stack.pop(), stack.pop()) {
                    stack.push(a - b);
                } else {
                    panic!("Invalid expression");
                }
            }
            "*" => {
                if let (Some(b), Some(a)) = (stack.pop(), stack.pop()) {
                    stack.push(a * b);
                } else {
                    panic!("Invalid expression");
                }
            }
            "/" => {
                if let (Some(b), Some(a)) = (stack.pop(), stack.pop()) {
                    stack.push(a / b);
                } else {
                    panic!("Invalid expression");
                }
            }
            _ => {
                if let Ok(number) = token.parse::<i32>() {
                    stack.push(number);
                } else {
                    panic!("Invalid token: {}", token);
                }
            }
        }
    }

    if let Some(result) = stack.pop() {
        if stack.is_empty() {
            return result;
        } else {
            panic!("Invalid expression");
        }
    } else {
        panic!("Invalid expression");
    }
}

fn main() {
    let infix_expression = "1 + 2 * 3";
    let rpn_expression = infix_to_rpn(infix_expression);
    println!("逆波兰表达式: {}", rpn_expression);

    let result = evaluate_rpn(&rpn_expression);
    println!("计算结果: {}", result);
}
